package org.sibframework.sda.ids;

import java.sql.SQLException;
import java.util.HashMap;

public class IdGenFactory {
	private static HashMap<Class<?>, IdGen<?>> cache = new HashMap<Class<?>, IdGen<?>>();
	
	public static <T> IdGen<T> getIdGen(Class<T> clas) throws SQLException {
		IdGen<T> idGen = (IdGen<T>) cache.get(clas);
		if (idGen != null) {
			return idGen;
		} else {
			return getSafeIdGen(clas);
		}
	}
	
	private synchronized static <T> IdGen<T> getSafeIdGen(Class<T> clas) throws SQLException {
		IdGen<T> idGen = (IdGen<T>) cache.get(clas);
		if (idGen == null) {
			idGen = new IdGen<T>(clas);
			cache.put(clas, idGen);
		}
		return idGen;
	}
}
